﻿using Sunny.UI;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace CoreShop.App
{
    [ToolboxItem(true)]
    [DefaultEvent("ValueChanged")]
    public partial class UIKeyboardTextBox : UIDropControl, IToolTip, IHideDropDown
    {
        public UIKeyboardTextBox()
        {
            InitializeComponent();
            edit.KeyDown += Edit_KeyDown;
            edit.Leave += Edit_Leave;
            fullControlSelect = true;
        }

        private void Edit_Leave(object sender, EventArgs e)
        {
            HideDropDown();
        }

        private void Edit_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up)
            {
                //if (!NumPadForm.Visible)  ShowDropDown();
            }
            else if (e.KeyCode == Keys.Escape)
            {
                NumPadForm.Close();
            }
            else if (e.KeyCode == Keys.Return)
            {
                ValueChanged?.Invoke(this, Text);
                if (NumPadForm.Visible)
                {                    
                    NumPadForm.Close();
                }
                //else
                //{
                //    ShowDropDown();
                //}
            }
            else
            {
                base.OnKeyDown(e);
            }
        }

        private void ShowDropDown()
        {
            NumPadForm.AutoClose = false;
            numPad.SetDPIScale();
            numPad.SetStyleColor(UIStyles.ActiveStyleColor);

            if (!NumPadForm.Visible)
            {
                NumPadForm.Show(edit.Parent.Parent, 0, edit.Height+100);
            }

            edit.Focus();
        }

        public Control ExToolTipControl()
        {
            return edit;
        }

        public void HideDropDown()
        {
            try
            {
                if (NumPadForm != null && NumPadForm.Visible)
                    NumPadForm.Close();
            }
            catch
            {
            }
        }

        private readonly UIKeyboardItem numPad = new UIKeyboardItem();

        private UIDropDown numPadForm;

        private UIDropDown NumPadForm
        {
            get
            {
                if (numPadForm == null)
                {
                    numPadForm = new UIDropDown(numPad);

                    if (numPadForm != null)
                    {
                        numPadForm.VisibleChanged += NumBoardForm_VisibleChanged;
                        numPadForm.ValueChanged += NumBoardForm_ValueChanged;
                    }
                }

                return numPadForm;
            }
        }

        public const int WM_CHAR = 256;
        private void NumBoardForm_ValueChanged(object sender, object value)
        {
            int start = edit.SelectionStart;
            switch ((int)value)
            {
                case 95:
                    edit.Text = edit.Text.Left(start) + "_" + edit.Text.Middle(start, edit.Text.Length);
                    start += 1;
                    edit.SelectionStart = start;
                    edit.Select(start, 0);
                    break;
                case 13:
                    ValueChanged?.Invoke(this, Text);
                    break;
                default:
                    Sunny.UI.Win32.User.PostMessage(edit.Handle, WM_CHAR, (int)value, 0);
                    edit.SelectionStart = start;
                    edit.Select(start, 0);
                    //this.Focus();
                    break;
            }
        }

        public delegate void OnValueChanged(object sender, string value);
        public event OnValueChanged ValueChanged;

        private void NumBoardForm_VisibleChanged(object sender, System.EventArgs e)
        {
            dropSymbol = SymbolNormal;
            if (NumPadForm.Visible)
            {
                dropSymbol = SymbolDropDown;
            }

            Invalidate();
        }

        private void UIKeyboardTextBox_ButtonClick(object sender, EventArgs e)
        {
            if (NumPadForm.Visible)
            {
                NumPadForm.Close();
            }
            else
            {
                ShowDropDown();
            }
        }

        private int symbolSize = 24;
        public int SymbolSize
        {
            get => symbolSize;
            set
            {
                symbolSize = value;
                Invalidate();
            }
        }

        protected override void OnPaintFore(Graphics g, GraphicsPath path)
        {
            g.FillRectangle(GetFillColor(), new Rectangle(Width - Padding.Right, 2, Padding.Right - 1, Height - 4));
            Color color = GetRectColor();
            g.DrawFontImage(dropSymbol, SymbolSize, color, new Rectangle(Width - Padding.Right, 0, Padding.Right, Height), 1, 0);
        }
    }
}
